package rollercoaster.railway;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import rollercoaster.railway.interfaces.BinarioDataReader;

public class XMLBinarioDataReader implements BinarioDataReader{
	

	private HashMap<String, String> trajectoryInfo=new HashMap<String, String>();
	

	private HashMap<String, String> supportPositionInfo=new HashMap<String, String>();

	private HashMap<String, HashMap<String, String>> textureInfo=new HashMap<String, HashMap<String, String>>();
	
	private HashMap<String, HashMap<String,String>> tubesInfo=new HashMap<String, HashMap<String,String>>();
	
	public XMLBinarioDataReader(String filePath) {
		super();
		
		File xmlFile=new File(filePath);
		DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
		org.w3c.dom.Document doc=null;
		try {
			DocumentBuilder builder=dbFactory.newDocumentBuilder();
			doc=builder.parse(xmlFile);
			doc.getDocumentElement().normalize();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		NodeList traj=doc.getElementsByTagName("trajectory");
		Node trajectoryNode=traj.item(0);
		Element trajElement=(Element) trajectoryNode;
		trajectoryInfo.put("base-trajectory", trajElement.getElementsByTagName("base-trajectory").item(0).getTextContent());
		trajectoryInfo.put("up-trajectory", trajElement.getElementsByTagName("up-trajectory").item(0).getTextContent());
		
		NodeList supp=doc.getElementsByTagName("support-position");
		Node suppNode=supp.item(0);
		Element suppElement=(Element) suppNode;
		supportPositionInfo.put("support-position", suppElement.getTextContent());
		
	
		NodeList textureList=doc.getElementsByTagName("texture");
		
		for (int i = 0; i < textureList.getLength(); i++) {
			Node text=textureList.item(i);
			Element texEl=(Element) text;
			textureInfo.put(texEl.getElementsByTagName("texture-name").item(0).getTextContent(), new HashMap<String, String>());
			textureInfo.get(texEl.getElementsByTagName("texture-name").item(0).getTextContent()).put("texture-file", texEl.getElementsByTagName("texture-file").item(0).getTextContent());
			Element tr=(Element) texEl.getElementsByTagName("texture-transform").item(0);
			textureInfo.get(texEl.getElementsByTagName("texture-name").item(0).getTextContent()).put("txa", tr.getAttribute("a"));
			textureInfo.get(texEl.getElementsByTagName("texture-name").item(0).getTextContent()).put("txb", tr.getAttribute("b"));
			textureInfo.get(texEl.getElementsByTagName("texture-name").item(0).getTextContent()).put("txc", tr.getAttribute("c"));
			textureInfo.get(texEl.getElementsByTagName("texture-name").item(0).getTextContent()).put("txd", tr.getAttribute("d"));
		}
		
		
		NodeList  tubeList=doc.getElementsByTagName("tube");
		
		for (int i = 0; i < tubeList.getLength(); i++) {
			Node tube=tubeList.item(i);
			Element tubeEl=(Element) tube;
			HashMap<String, String> map=new HashMap<String, String>();
			map.put("tube-ray", tubeEl.getElementsByTagName("tube-ray").item(0).getTextContent());
			map.put("texture-name", tubeEl.getElementsByTagName("texture-name").item(0).getTextContent());
			tubesInfo.put(tubeEl.getElementsByTagName("tube-name").item(0).getTextContent(),map);
		}
	}
	


	public HashMap<String, String> getTrajectoryInfo() {
		return trajectoryInfo;
	}

	public HashMap<String, String> getSupportPositionInfo() {
		// TODO Auto-generated method stub
		return supportPositionInfo;
	}

	public HashMap<String, HashMap<String, String>> getTextureInfo() {
		return textureInfo;
	}

	public HashMap<String, String> getTubeParameters(String tubename) {
		// TODO Auto-generated method stub
		return tubesInfo.get(tubename);
	}

}
